
//获取所有存储信息
//点击某个按钮就获取哪个的数据
//起始获取第一个的数据
//没有就是空档存
//需要显示主角生命，防御，攻击,钥匙，当前层
//可以进行档存删除
//双击进入档存

using System;
using UnityEngine;
using UnityEngine.UI;

public class ChooseCopyDataPanel : BasePanel
{
    [Header("按钮")]
    public Button closeButton;
    public Button data1Button;
    public Button data2Button;
    public Button data3Button;
    public Button data4Button;
    public Button data5Button;
    public Button data6Button;
    public Button copyButton;
    [Header("页面显示")]
    public Text hpText;
    public Text armorText;
    public Text damageText;
    public Text yellowKeyText;
    public Text redKeyText;
    public Text blueKeyText;
    public Text currLevelText;
    [Header("页面和无数据")]
    public GameObject pageObj;
    public Text noDataText;
    public GameObject winObj;
    public GameObject dieObj;
    [Header("数据选择")]
    public GameObject dataChoose;

    //是否已经进行覆盖
    public bool isOver = false;
    //存储点位
    public int saveNum;
    //当前数据
    public int currData = 1;

    public override void OnClose()
    {
        base.OnClose();

        GameManager.Instance.canOprate=true;
    }
    public override void OnInit()
    {
        base.OnInit();
        path="UI/Panel/ChooseCopyDataPanel";
        type=PanelType.Panel;

        GameManager.Instance.canOprate=false;
    }
    public override void OnShow(params object[] par)
    {
        base.OnShow(par);
        saveNum=(int)par[0];

        if(saveNum==1)
        {
            if(GameManager.Instance.save1)
            {
                isOver=true;
            }
        }
        else
        {
            if (GameManager.Instance.save2)
            {
                isOver=true;
            }
        }

        SeekUI();
        //初始打开数据1
        Data1Button();
    }
    //找到UI
    public void SeekUI()
    {
        //寻找
        closeButton=item.transform.Find("Close").GetComponent<Button>();
        copyButton=item.transform.Find("Copy").GetComponent<Button>();
        data1Button=item.transform.Find("Data1").GetComponent<Button>();
        data2Button=item.transform.Find("Data2").GetComponent<Button>();
        data3Button=item.transform.Find("Data3").GetComponent<Button>();
        data4Button=item.transform.Find("Data4").GetComponent<Button>();
        data5Button=item.transform.Find("Data5").GetComponent<Button>();
        data6Button=item.transform.Find("Data6").GetComponent<Button>();
        pageObj=item.transform.Find("Back/Page").gameObject;
        dataChoose=item.transform.Find("DataChoose").gameObject;
        noDataText=item.transform.Find("Back/No").GetComponent<Text>();
        dieObj=item.transform.Find("Back/Die").gameObject;
        winObj=item.transform.Find("Back/Win").gameObject;
        hpText=item.transform.Find("Back/Page/Hp").GetComponent<Text>();
        armorText=item.transform.Find("Back/Page/Armor").GetComponent<Text>();
        damageText=item.transform.Find("Back/Page/Damage").GetComponent<Text>();
        currLevelText=item.transform.Find("Back/Page/CurrLevel").GetComponent<Text>();
        yellowKeyText=item.transform.Find("Back/Page/YellowKey").GetComponent<Text>();
        redKeyText=item.transform.Find("Back/Page/RedKey").GetComponent<Text>();
        blueKeyText=item.transform.Find("Back/Page/BlueKey").GetComponent<Text>();
        //按钮赋值
        closeButton.onClick.AddListener(CloseButton);
        copyButton.onClick.AddListener(CopyButton);
        data1Button.onClick.AddListener(Data1Button);
        data2Button.onClick.AddListener(Data2Button);
        data3Button.onClick.AddListener(Data3Button);
        data4Button.onClick.AddListener(Data4Button);
        data5Button.onClick.AddListener(Data5Button);
        data6Button.onClick.AddListener(Data6Button);
        //隐藏
    }
    #region 按钮
    //关闭
    public void CloseButton()
    {
        Close();
        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //复制
    public void CopyButton()
    {
        isOver=true;

        //且此存储无用
        if(saveNum==1)
        {
            GameManager.Instance.save1=true;
        }
        else
        {
            GameManager.Instance.save2=true;
        }

        //保存一下所有数据
        SaveManager.instance.SaveAllData();

        //直接进行一次复制，并重写加载此存储
        GameManager.Instance.CopyFile(GameManager.Instance.fileNum, currData);
        //更新UI数据
        GetData(currData);
    }
    //数据1
    public void Data1Button()
    {
        currData=1;
        GetData(1);
        dataChoose.transform.position=data1Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //数据2
    public void Data2Button()
    {
        currData=2;
        GetData(2);
        dataChoose.transform.position=data2Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //数据3
    public void Data3Button()
    {
        currData=3;
        GetData(3);
        dataChoose.transform.position=data3Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //数据4
    public void Data4Button()
    {
        currData=4;
        GetData(4);
        dataChoose.transform.position=data4Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //数据5
    public void Data5Button()
    {
        currData=5;
        GetData(5);
        dataChoose.transform.position=data5Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //数据6
    public void Data6Button()
    {
        currData=6;
        GetData(6);
        dataChoose.transform.position=data6Button.transform.position;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    #endregion
    //获取数据
    public void GetData(int levelNum)
    {
        //加载主数据
        MainData mainData=FileHandler.Load<MainData>(Application.persistentDataPath, "Data" + levelNum + "/MainData");
        //更新数据
        UpdateData(mainData);
    }
    //更新数据
    public void UpdateData(MainData data)
    {
        if (data!=null)
        {
            if (data.isDieOver)
            {
                winObj.SetActive(false);
                dieObj.SetActive(true);
                UpdatePabelData(data, false);
            }
            else if (data.isWinOver)
            {
                winObj.SetActive(true);
                dieObj.SetActive(false);
                UpdatePabelData(data, false);
            }
            else
            {
                winObj.SetActive(false);
                dieObj.SetActive(false);

                UpdatePabelData(data, true);
            }
        }
        else
        {
            pageObj.SetActive(false);
            noDataText.gameObject.SetActive(true);
            winObj.SetActive(false);
            dieObj.SetActive(false);
            copyButton.gameObject.SetActive(true);
        }

        //如果和页面相同
        if (currData==GameManager.Instance.fileNum)
        {
            copyButton.gameObject.SetActive(false);
        }
        else
        {
            copyButton.gameObject.SetActive(true);
        }
        if(isOver)
        {
            copyButton.gameObject.SetActive(false);
        }
    }
    //更新面板数据
    public void UpdatePabelData(MainData data, bool isEnter)
    {
        pageObj.SetActive(true);
        noDataText.gameObject.SetActive(false);

        hpText.text = data.playerCurrHp.ToString();
        armorText.text = data.armor.ToString();
        damageText.text = data.damage.ToString();
        yellowKeyText.text = data.yellowKeyNum.ToString();
        redKeyText.text = data.redKeyNum.ToString();
        blueKeyText.text = data.blueKeyNum.ToString();
        currLevelText.text = data.quitIdLevel.ToString();
        copyButton.gameObject.SetActive(isEnter);
    }
}